package com.livepan.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.ChapterDOMapper;
import com.livepan.server.dataobject.ChapterDO;
import com.livepan.server.dataobject.ChapterDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.ChapterModel;
import com.livepan.server.service.model.ChapterPageModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.CharsetMapping;
import sun.swing.StringUIClientPropertyKey;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChapterService {
    @Autowired
    private ChapterDOMapper chapterDOMapper;

    @Autowired
    private ValidatorImpl validator;

    public void list(ChapterPageModel chapterPageModel) {
        PageHelper.startPage(chapterPageModel.getPage(), chapterPageModel.getSize());
        ChapterDOExample chapterDOExample = new ChapterDOExample();

        ChapterDOExample.Criteria criteria = chapterDOExample.createCriteria();
        if (!StringUtils.isNullOrEmpty(chapterPageModel.getCourseId())) {
            criteria.andCourseIdEqualTo(chapterPageModel.getCourseId());
        }
        List<ChapterDO> chapterDOList = chapterDOMapper.selectByExample(chapterDOExample);
        PageInfo<ChapterDO> pageInfo = new PageInfo<>(chapterDOList);
        chapterPageModel.setTotal(pageInfo.getTotal());
        List<ChapterModel> chapterModelList = chapterDOList.stream().map(
                this::convertChapterModelFromChapterDO
        ).collect(Collectors.toList());
        chapterPageModel.setList(chapterModelList);
    }

    public void save(ChapterModel chapterModel) throws BusinessException {
        ValidationResult validateResult = validator.validate(chapterModel);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        ChapterDO chapterDO = CopyUtil.copy(chapterModel, ChapterDO.class);
        if (chapterDOMapper.selectByPrimaryKey(chapterDO.getId()) == null) {
            this.insert(chapterDO);
        } else {
            this.update(chapterDO);
        }
    }

    private void insert(ChapterDO chapterDO) {
        chapterDO.setId(UUIDUtil.getShortUUID());
        chapterDOMapper.insert(chapterDO);
    }

    private void update(ChapterDO chapterDO) {
        chapterDOMapper.updateByPrimaryKey(chapterDO);
    }

    public void delete(String id) {
        chapterDOMapper.deleteByPrimaryKey(id);
    }


    private ChapterModel convertChapterModelFromChapterDO(ChapterDO chapterDO) {
        if (chapterDO == null) {
            return null;
        }
        ChapterModel chapterModel = new ChapterModel();
        BeanUtils.copyProperties(chapterDO, chapterModel);
        return chapterModel;
    }
}
